import { customTable } from '@/components/ProTable/interface';
import { ProTableValue, PublicType } from '@/interface/public';

import { formOpen } from '@/utils/form';
import { useAddForm } from './use-addForm';
import { PermissionInterface } from '@/api/interface/auth/permission';
import { DictionariesInterface } from '@/api/interface/basic/dictionaries';

export function useDataGrid(proTableBox: ProTableValue) {
  const { addForm, saveAddForm, getAddFormChildren } = useAddForm(proTableBox);
  let dataGrid = reactive<customTable>({
    initParam: {
      id: 0
    },
    expandRowKeys: [],
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'right', width: 160 },

      {
        prop: 'label',
        label: '字典名称',
        width: 220
      },
      {
        prop: 'code',
        label: '编号'
      },
      {
        prop: 'value',
        label: '字典值'
      },
      {
        prop: 'sort',
        label: '排序'
      },
      {
        prop: 'pid',
        label: '父级id'
      },

      {
        prop: 'remark',
        label: 'remark'
      }
    ],
    headButtonList: [
      {
        label: '新增',
        type: 'primary',
        icon: 'Plus',
        // permissionCode: 'p:auth:user:save',
        methods: async () => {
          formOpen<DictionariesInterface.ReqSave>(addForm, '新增字典');
          getAddFormChildren();
        }
      }
    ],
    rowButtonList: [
      {
        label: 'edit',
        type: 'primary',
        // permissionCode: 'p:auth:user:save',
        methods: async row => {
          const params = {
            ...row
          };
          if (row.editRoleId) {
            const roleIds = row.editRoleId.split(',');
            const ids = [];
            for (let id of roleIds) {
              ids.push(parseInt(id));
            }
            params.editRoleId = ids;
          }

          console.log(params.editRoleId, ' params.editRoleId');

          formOpen<DictionariesInterface.ReqSave>(addForm, '编辑字典', params);
          getAddFormChildren();
        },
        attrs: {
          link: true
        }
      },
      {
        label: '追加',
        type: 'primary',
        // permissionCode: 'p:auth:user:save',
        methods: async row => {
          const params = {
            pid: row.id
          };
          formOpen<DictionariesInterface.ReqSave>(addForm, '追加字典', params);
          getAddFormChildren();
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'enable',
        type: 'primary',
        // permissionCode: 'p:auth:user:status',
        display: item => item.status !== BasicKey.ENABLE,
        methods: (row: DictionariesInterface.ResList) => {
          const params = { id: row.id, status: BasicKey.ENABLE };
          useHandleData(
            basicDataDictionaryUpdateStatus,
            params,
            `${transitionLanguages('table', 'disabled')}【${row.label}】${transitionLanguages('table', 'user')}`,
            proTableBox.value?.getTableList
          );
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'disabled',
        type: 'danger',
        display: item => item.status !== BasicKey.DISABLE,
        methods: (row: DictionariesInterface.ResList) => {
          const params = { id: row.id, status: BasicKey.DISABLE };
          useHandleData(
            basicDataDictionaryUpdateStatus,
            params,
            `${transitionLanguages('table', 'disabled')}【${row.label}】${transitionLanguages('table', 'user')}`,
            proTableBox.value?.getTableList
          );
        },
        attrs: {
          link: true
        }
      },
      {
        label: '删除',
        type: 'danger',
        // icon: 'svg-del',
        // permissionCode: 'maintain:remove',
        // tooltip: '删除',
        display: row => row.status === BasicKey.ENABLE,
        methods: (row: DictionariesInterface.ResList) => {
          const params = { id: row.id };
          useHandleData(basicDataDictionaryDelete, params, `删除 【${row.label}】字典`, proTableBox.value.getTableList);
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  /**
   * 对显示的数据进行回调处理
   */
  const dataCallBack = (data: DictionariesInterface.ResList[]) => {
    // data.forEach(item => {
    //   item.hasChildren = !!(item?.children && item?.children.length > 0);
    // });
    console.log(data, 'data');

    return data;
  };

  return { dataGrid, dataCallBack, addForm, saveAddForm };
}
